package org.ccf.certified.controller.shiro;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.ccf.certified.bean.ResponseBean;
import org.ccf.certified.bean.pagination.PageRequest;
import org.ccf.certified.business.log.annotation.LogAnnotation;
import org.ccf.certified.business.shiro.manager.RoleManager;
import org.ccf.certified.business.shiro.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiuFang
 * @date 2022/1/12
 */
@RestController
@RequestMapping("/web/api/role")
public class RoleController {

    @Autowired
    private RoleManager roleManager;


    @LogAnnotation(module = "新增或修改角色信息")
    @PostMapping
    public ResponseBean save(@RequestBody Role role){
        boolean save = roleManager.saveOrUpdate(role);
        return save ? ResponseBean.getResultsSuccess() : ResponseBean.getResultsFail();
    }

    @LogAnnotation(module = "删除角色信息")
    @DeleteMapping
    public ResponseBean del(@RequestParam Long id){
        boolean save = roleManager.removeById(id);
        return save ? ResponseBean.getResultsSuccess() : ResponseBean.getResultsFail();
    }

    @LogAnnotation(module = "分页查询角色信息")
    @GetMapping("/page")
    public ResponseBean page(PageRequest pageRequest) {
        IPage page = roleManager.page(pageRequest.convertToPage());
        return ResponseBean.getResultsSuccess(page);
    }

    @LogAnnotation(module = "根据主键查询角色")
    @GetMapping("/{id}")
    public ResponseBean getById(@PathVariable Long id){
        Role role = roleManager.getById(id);
        return ResponseBean.getResultsSuccess(role);
    }
}
